<template>
	<view>
		<view class="page-foot bg-white" @click="applyVillage()">
			<view class="g-btn3 mlrauto mtb10">申请添加小区</view>
		</view>
		<view class="container p30">
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
				:down="downOption" :up="upOption">
			<view class="ptb30 mb20 flex-box m-hairline--bottom" v-for="(item,index) in villageList">
				<view class="flex-grow-1">
					<view class="fs32 col10 mb25">{{item.village.name}}</view>
					<view class="fs24 col89">{{item.village.prov}}{{item.village.city}}{{item.village.region}}{{item.village.street}}{{item.village.addr}}</view>
				</view>
				<view v-if="fromType=='add_rental'" class="btn-remove" @click="chooseVillage(index)">选择</view>
				<view v-else class="btn-remove" @click="removeVillage(index)">移除</view>
			</view>
			<mescroll-empty v-if="villageList.length<=0"></mescroll-empty>
			</mescroll-body>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	const core = require('@/xilu/core.js');
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				fromType:'',
				villageList: [],
				downOption:{
					auto:false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption:{
					auto:true, // 不自动加载
					// page: {
					// 	num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						icon: '/static/images/none-data.png',
						tip: '暂无会员卡安排哦' 
					}
				},
			}
		},
		onLoad(options){
			this.fromType = options.fromType || '';
			//this.fetch();
		},
		methods: {
			downCallback(){
				this.mescroll.resetUpScroll()
			},
			upCallback(page){
				let that = this;
				let postData = {
				    page: page.num, // 页码, 默认从1开始
				    pagesize: page.size, // 每页数据条数
		
				};
				//联网加载数据
				core.get({url:'/api/xiluhouse.broker/my_village',data:postData,success:(ret)=>{
					 if(page.num == 1) that.villageList = []; //如果是第一页需手动制空列表
					 that.villageList = that.villageList.concat(ret.data.data); //追加新数据
					 //联网成功的回调,隐藏下拉刷新和上拉加载的状态;
					 that.mescroll.endSuccess(ret.data.data.length);
				},loading:false});
			},
			//删除
			removeVillage(index){
				var that = this;
				let villageList = this.villageList;
				uni.showModal({
					title:'提示',
					content:'确认移除',
					success: function(res){
						if(res.confirm){
							core.post({url:'/api/xiluhouse.broker/remove_village',data:{village_broker_id:villageList[index].id},success:(ret)=>{
								villageList.splice(index,1);
								that.villageList = villageList;
							}});
						}
					}
				})
			},
			//申请
			applyVillage(){
				uni.navigateTo({
					url: '/pages/rental/apply_add_community/apply_add_community?type=add'
				})
			},
			//选择小区
			chooseVillage(index){
				let village = this.villageList[index];
				uni.$emit("chooseVillage",village.village);
				uni.navigateBack({delta:1});
			},
		}
	}
</script>

<style lang="less" scoped>
	.btn-remove {
		width: 110rpx;
		height: 55rpx;
		border-radius: 30rpx;
		border: 1px solid var(--normal);
		font-size: 24rpx;
		color: var(--normal);
		line-height: 55rpx;
		text-align: center;
	}
</style>
